package test.redis.tool;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Properties;
import java.util.Set;

import org.apache.commons.pool2.impl.GenericObjectPoolConfig;

import redis.clients.jedis.JedisSentinelPool;
import redis.clients.jedis.JedisShardInfo;
import redis.clients.jedis.ShardedJedisPool;

public class RedisConfig {
    public static ShardedJedisPool buildSharded(String config) throws IOException {
        GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();
        List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>(1);
        Properties prop = new Properties();
        InputStream input = ResourceUtils.getResourceAsStream(config);
        prop.load(input);

        String host = prop.getProperty("redis.url");
        int port = Integer.valueOf(prop.getProperty("redis.port"));
        String auth = prop.getProperty("redis.auth");
        JedisShardInfo jedisShardInfo = new JedisShardInfo(host, port);
        jedisShardInfo.setPassword(auth);
        shards.add(jedisShardInfo);
        int maxTotal = Integer.valueOf(prop.getProperty("redis.maxTotal"));
        int maxWaitMillis = Integer.valueOf(prop.getProperty("redis.maxWaitMillis"));
        int maxIdle = Integer.valueOf(prop.getProperty("redis.maxIdle"));
        int minIdle = Integer.valueOf(prop.getProperty("redis.minIdle"));
        poolConfig.setMaxTotal(maxTotal);
        poolConfig.setMaxWaitMillis(maxWaitMillis);
        poolConfig.setMaxIdle(maxIdle);
        poolConfig.setMinIdle(minIdle);
        poolConfig.setTestOnBorrow(false);
        poolConfig.setTestOnReturn(false);
        return new ShardedJedisPool(poolConfig, shards);
    }

    public static JedisSentinelPool build(String config) throws IOException {
        GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();
        Properties prop = new Properties();
        InputStream input = ResourceUtils.getResourceAsStream(config);
        prop.load(input);

        String host = prop.getProperty("redis.url");
        System.out.println(host);
        // int port = Integer.valueOf(prop.getProperty("redis.port"));
        String auth = prop.getProperty("redis.auth");
        int maxTotal = Integer.valueOf(prop.getProperty("redis.maxTotal"));
        int maxWaitMillis = Integer.valueOf(prop.getProperty("redis.maxWaitMillis"));
        int maxIdle = Integer.valueOf(prop.getProperty("redis.maxIdle"));
        int minIdle = Integer.valueOf(prop.getProperty("redis.minIdle"));
        int database = Integer.valueOf(prop.getProperty("redis.database"));
        String masterName = prop.getProperty("redis.masterName");
        poolConfig.setMaxTotal(maxTotal);
        poolConfig.setMaxWaitMillis(maxWaitMillis);
        poolConfig.setMaxIdle(maxIdle);
        poolConfig.setMinIdle(minIdle);
        poolConfig.setTestOnBorrow(true);
        Set<String> sentinels = new HashSet<String>();
        for (String sentinel : host.split(",")) {
            sentinels.add(sentinel);
        }
        return new JedisSentinelPool(masterName, sentinels, poolConfig, maxWaitMillis, auth, database);
    }

    public static JedisSentinelPool build() throws IOException {
        GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();
        // Properties prop = new Properties();
        // InputStream input = ResourceUtils.getResourceAsStream(config);
        // prop.load(input);
        //
        // String host = prop.getProperty("redis.url");
        // System.out.println(host);
        // // int port = Integer.valueOf(prop.getProperty("redis.port"));
        // String auth = prop.getProperty("redis.auth");
        // int maxTotal = Integer.valueOf(prop.getProperty("redis.maxTotal"));
        // int maxWaitMillis =
        // Integer.valueOf(prop.getProperty("redis.maxWaitMillis"));
        // int maxIdle = Integer.valueOf(prop.getProperty("redis.maxIdle"));
        // int minIdle = Integer.valueOf(prop.getProperty("redis.minIdle"));
        // int database = Integer.valueOf(prop.getProperty("redis.database"));
        // String masterName = prop.getProperty("redis.masterName");
        poolConfig.setMaxTotal(RedisToolConfig.maxTotal);
        poolConfig.setMaxWaitMillis(RedisToolConfig.maxWaitMillis);
        poolConfig.setMaxIdle(RedisToolConfig.maxIdle);
        poolConfig.setMinIdle(RedisToolConfig.minIdle);
        poolConfig.setTestOnBorrow(true);
        Set<String> sentinels = new HashSet<String>();
        for (String sentinel : RedisToolConfig.host.split(",")) {
            sentinels.add(sentinel);
        }
        return new JedisSentinelPool(RedisToolConfig.masterName, sentinels, poolConfig, RedisToolConfig.maxWaitMillis,
                RedisToolConfig.auth, RedisToolConfig.database);
    }
}
